/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sisfo.astacala.controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import sisfo.astacala.form.FormMaster;

/**
 *
 * @author marifnst
 */
public class ControllerFormGrid extends ActionSupport implements ServletRequestAware {

    private InputStream inputStream;
    private int total;
    private int page;
    private int records;
    private String captionGrid;
    private String parentGrid;
    private String keySearch;
    private String gridUrl;
    private Map dataMap;
    private List colModel;
    private List colNames;
    private HttpServletRequest request;

    @Override
    public String execute() throws Exception {
        List colData;

        try {
//            generateParameter();        

            String queryColumn = String.format("from ObjectColumn where parent='%s'", request.getParameter("parent").toString());
            String queryData = String.format("select * from %s", "tbls_" + request.getParameter("parent").toString());
            List tempList = FormMaster.getInstance().getGridData(queryColumn, queryData);

            Map session = ActionContext.getContext().getSession();
            session.put("parentGrid", request.getParameter("parent"));

            colNames = (List) tempList.get(0);
            colModel = (List) tempList.get(1);
            colData = (List) tempList.get(2);
            captionGrid = request.getParameter("parent").toUpperCase();
            parentGrid = request.getParameter("parent");
            keySearch = tempList.get(3).toString();
            gridUrl = String.format("/ASTACALA/getGrid?parent=%s", parentGrid);

            dataMap = new HashMap();
            dataMap.put("datagrid", colData);

            total = 10;
            page = 1;
            records = 10;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    public void generateParameter() {
        System.out.println("===");
        Enumeration enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String parameterName = enumeration.nextElement().toString();
            String parameterValue = request.getParameter(parameterName);
            System.out.println(parameterName + " : " + parameterValue);
        }
        System.out.println("===");
    }

    public String addGridProcess() {
//        generateParameter();
        Map session = ActionContext.getContext().getSession();
        FormMaster.getInstance().addGridData(request, String.format("tbls_%s", session.get("parentGrid")));
        return SUCCESS;
    }

    public String editGridProcess() {
//        generateParameter();
        Map session = ActionContext.getContext().getSession();
        dataMap = FormMaster.getInstance().editGridData(request, String.format("tbls_%s", session.get("parentGrid")));
        return SUCCESS;
    }

    public String deleteGridProcess() {
//        generateParameter();
        Map session = ActionContext.getContext().getSession();
        FormMaster.getInstance().deleteGridData(String.format("tbls_%s", session.get("parentGrid")), request.getParameter("id"));
        return SUCCESS;
    }

    public String searchGridProcess() {
        List colData;

        try {
//            generateParameter();
            Map session = ActionContext.getContext().getSession();
            String parentName = session.get("parentGrid").toString();
            String queryColumn = String.format("from ObjectColumn where parent='%s'", parentName);
            String queryData = String.format("SELECT * FROM %s WHERE %s like '%s'", "tbls_" + parentName,
                    request.getParameter("key").toLowerCase(),
                    "%" + request.getParameter("keyvalue").toLowerCase() + "%");

            //            System.out.println("parent name : " + parentName);
            List tempList = FormMaster.getInstance().getGridData(queryColumn, queryData);

            colNames = (List) tempList.get(0);
            colModel = (List) tempList.get(1);
            colData = (List) tempList.get(2);
            captionGrid = parentName.toUpperCase();
            parentGrid = parentName;
            keySearch = tempList.get(3).toString();
            gridUrl = String.format("/ASTACALA/form/searchForm?key=%s&keyvalue=%s", 
                    request.getParameter("key").toLowerCase(),
                    request.getParameter("keyvalue"));

            dataMap = new HashMap();
            dataMap.put("datagrid", colData);

            total = 10;
            page = 1;
            records = 10;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    public String exportGridProcess() {
//        generateParameter();
        Map session = ActionContext.getContext().getSession();
        FormMaster.getInstance().exportGridData(request, session.get("parentGrid").toString());
        return SUCCESS;
    }
  
    //<editor-fold desc="getter">
    public int getTotal() {
        return total;
    }

    public int getPage() {
        return page;
    }

    public int getRecords() {
        return records;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public List getColModel() {
        return colModel;
    }

    public List getColNames() {
        return colNames;
    }

    public Map getDataMap() {
        return dataMap;
    }

    public String getCaptionGrid() {
        return captionGrid;
    }

    public String getParentGrid() {
        return parentGrid;
    }

    public String getKeySearch() {
        return keySearch;
    }

    public String getGridUrl() {
        return gridUrl;
    }

    //</editor-fold>
    
    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        request = hsr;
    }
}
